package com.zxy.supplier_system.server.controller

import com.zxy.supplier_system.server.controller.GoodsController.GoodsDetail.SubGoodsResponse
import com.zxy.supplier_system.server.entity.Authority
import com.zxy.supplier_system.server.service.ProcurementReturnService
import com.zxy.supplier_system.server.utils.IntIdAndName
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.web.bind.annotation.*
import java.math.BigDecimal
import java.time.LocalDate
import java.time.OffsetDateTime

@RestController
@RequestMapping("procurementSuppliers/returnOrders")
class ProcurementReturnController(
    private val procurementReturnService: ProcurementReturnService,
) {

    class QueryProcurementReturnRequest(
        val supplierIds: List<Int> = emptyList(),
        val startDate: LocalDate?,
        val endDate: LocalDate?,
        val createdByIds: List<Int> = emptyList(),
//        val status: ProcurementReturn.Status?,
    )

    @HasAuthority(Authority.采购_退货_查询)
    @GetMapping
    fun queryProcurementReturn(queryProcurementReturnRequest: QueryProcurementReturnRequest, pageable: Pageable):Page<QueryProcurementReturnResponse> {
        return this.procurementReturnService.queryProcurementReturn(queryProcurementReturnRequest,pageable)
    }

    data class QueryProcurementReturnResponse(
        val id:Int,
        val createdBy: IntIdAndName,
        val createdDateTime: OffsetDateTime,
        val goodsKindAmount:Int,
        val supplier:IntIdAndName,
//        val status: ProcurementReturn.Status,
//        val receiptId:Int?,
        val number:String,
    )

    data class UpdateOrderGoodsRequest(
        val items:List<OrderGoodsItem>
    )

    data class OrderGoodsItem(
        val id:Int,
        val amount:Int
    )

    @HasAuthority(Authority.采购_退货_修改)
    @PatchMapping("{id}/goods")
    fun updateOrderGoods(@RequestBody updateOrderGoodsRequest: UpdateOrderGoodsRequest, @PathVariable id: Int,
    ){
        return this.procurementReturnService.updateOrderGoods(id,updateOrderGoodsRequest)
    }

    @HasAuthority(Authority.采购_退货_创建)
    @GetMapping("{id}")
    fun getProcurementReturnDetail(@PathVariable id: Int, includeItems:Boolean = false):ProcurementReturnDetail{
        return this.procurementReturnService.getProcurementReturnDetail(id,includeItems)
    }

    data class ProcurementReturnDetail(
        val id:Int,
        val createdDateTime: OffsetDateTime,
        val createdBy: IntIdAndName,
        val supplier:IntIdAndName,
        val items:List<ProcurementReturnItemResponse>,
        val submittedDateTime:OffsetDateTime?,
//        val receiptDateTime: OffsetDateTime?,
//        val receiptId:Int?,
    )

    data class ProcurementReturnItemResponse(
        val id:Int,
        val name: String,
        val barcode:String,
        val price: BigDecimal,
        val amount:Int,
        val createdDateTime: OffsetDateTime,
        val lastModifiedDateTime: OffsetDateTime?,
        val brand:IntIdAndName?,
        val category:IntIdAndName?,
        val factory:IntIdAndName?,
        val amountInOrder:Int,
        val boxSpecification:String?,
        val subGoods: SubGoodsResponse? = null,
        val note: String? = null,
        val returnPrice:BigDecimal,
    )

    @HasAuthority(Authority.采购_退货_查询)
    @GetMapping("employees", params = ["projection=summary"])
    fun getProcurementReturnCreatedEmployees():List<IntIdAndName>{
        return this.procurementReturnService.getAllEmployees()
    }


    @HasAuthority(Authority.采购_退货_创建)
    @PatchMapping("{id}/submit")
    fun submitOrder(@PathVariable id: Int ){
        return this.procurementReturnService.submitProcurementReturn(id)
    }

//    @HasAuthority(Authority.采购_退货_入库_入库)
//    @PostMapping("{id}/receipt")
//    fun receipt(@PathVariable id: Int ):Int{
//        return this.procurementReturnService.receipt(id).id
//    }

}
